"""
文件名：LoginPage.py
作用：封装ECShop登录页面
"""

from time import sleep
from common.Base import Base, open_browser
from selenium.webdriver.common.by import By


class LoginPage(Base):
    """表现层：制作每个元素的定位器"""
    _username_loc = (By.NAME, "username")  # 用户名输入框
    _password_loc = (By.NAME, "password")  # 密码输入框
    _submit_loc = (By.NAME, "submit")  # 立即登录按钮
    _remember_loc = (By.ID, "remember")  # 记住密码
    _login_result_loc = (By.CLASS_NAME, "f4_b")  # 登录结果

    """操作层：对每一个元素进行的操作"""

    def input_username(self, username):
        """
        输入用户名
        :param username: 用户名
        :return:
        """
        self.send_keys(self._username_loc, username)

    def input_password(self, password):
        """
        输入密码
        :param password: 密码
        :return:
        """
        self.send_keys(self._password_loc, password)

    def remember_password(self):
        """
        记住密码
        :return:
        """
        self.click(self._remember_loc)

    def click_submit(self):
        """
        点击立即登录按钮
        :return:
        """
        self.click(self._submit_loc)

    def get_login_result(self):
        """
        获取登录结果
        :return:
        """
        return self.get_element_text(self._login_result_loc)

    def is_login_success(self, username):
        """
        判断用户是否登录成功
        :param username: 用户名
        :return: True/False
        """
        return self.is_text_equal_element(self._login_result_loc, username)


if __name__ == '__main__':
    login = LoginPage(open_browser())
    login.get_url("http://localhost:8080/ecshop/user.php")
    # 输入用户名
    login.input_username("test")
    # 输入密码
    login.input_password("zyy6282987")
    # 记住密码
    login.remember_password()
    # 点击立即登录按钮
    login.click_submit()
    # 关闭浏览器
    sleep(3)
    login.close_browser()
